package com.ruoyi.phoneController;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.domain.MobilEvaluate;
import com.ruoyi.domain.MobilMerchant;
import com.ruoyi.mapper.MobilEvaluateMapper;
import com.ruoyi.service.IMobilMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机端商户Controller
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/mobile/merchant")
public class MobilMerchantController extends BaseController {
    @Autowired
    private IMobilMerchantService mobilMerchantService;

    @Autowired
    private MobilEvaluateMapper mobilEvaluateMapper;

    /**
     * 查询附近门店列表
     */
    @GetMapping("/nearby")
    public AjaxResult getNearbyMerchants(
            @RequestParam String longitude,
            @RequestParam String latitude,
            @RequestParam(defaultValue = "50.0") Double radius,
            @RequestParam(required = false) Integer serviceType) {
        
        List<MobilMerchant> merchants = mobilMerchantService.selectNearbyMerchants(longitude, latitude, radius, serviceType);
        return success(merchants);
    }

    /**
     * 获取门店详情
     */
    @GetMapping("/{id}")
    public AjaxResult getMerchantDetails(@PathVariable Integer id) {
        MobilMerchant merchant = mobilMerchantService.getMerchantCompleteInfo(id);
        if (merchant == null) {
            return error("门店不存在");
        }
        return success(merchant);
    }

    /**
     * 获取门店评价列表
     */
    @GetMapping("/{id}/evaluates")
    public AjaxResult getMerchantEvaluates(@PathVariable Integer id) {
        List<MobilEvaluate> evaluates = mobilEvaluateMapper.selectEvaluatesByMerchantId(id);
        return success(evaluates);
    }

    /**
     * 查询门店列表（支持搜索）
     */
    @GetMapping("/list")
    public AjaxResult getMerchantList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String operatingArea,
            @RequestParam(required = false) Integer status) {
        
        MobilMerchant merchant = new MobilMerchant();
        merchant.setName(name);
        merchant.setOperatingArea(operatingArea);
        merchant.setStatus(status);
        merchant.setDeleted(0);
        merchant.setAuditStatusId(1);
        
        List<MobilMerchant> merchants = mobilMerchantService.selectMobilMerchantList(merchant);
        return success(merchants);
    }

    /**
     * 根据服务类型查询门店列表
     */
    @GetMapping("/by-service-type/{typeId}")
    public AjaxResult getMerchantsByServiceType(
            @PathVariable Integer typeId,
            @RequestParam(required = false) String longitude,
            @RequestParam(required = false) String latitude) {
        
        if (longitude != null && latitude != null) {
            // 如果提供了位置信息，查询附近门店
            List<MobilMerchant> merchants = mobilMerchantService.selectNearbyMerchants(longitude, latitude, 100.0, typeId);
            return success(merchants);
        } else {
            // 如果没有位置信息，查询所有提供该服务的门店
            MobilMerchant merchant = new MobilMerchant();
            merchant.setDeleted(0);
            merchant.setAuditStatusId(1);
            merchant.setStatus(0);
            List<MobilMerchant> merchants = mobilMerchantService.selectMobilMerchantList(merchant);
            return success(merchants);
        }
    }
}










